﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using kegg_service;
using Excel = Microsoft.Office.Interop.Excel;
using System.Net;
using System.Xml;
using System.Runtime.InteropServices;
using System.Diagnostics;
using QuickGraph;
using System.Runtime.Serialization.Formatters.Binary;
using MySql.Data.MySqlClient;

namespace BPP
{
    public partial class _programm
    {
        public class shared_functions
        {
            public static BidirectionalGraph<object, IEdge<object>> model_graph = new BidirectionalGraph<object, IEdge<object>>();
            public static BidirectionalGraph<object, IEdge<object>> constructed_graph = new BidirectionalGraph<object, IEdge<object>>();

            // Convert an object to a byte array
            public static byte[] ObjectToByteArray(Object obj)
            {
                if (obj == null)
                    return null;
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
            
            // Convert a byte array to an Object
            public static Object ByteArrayToObject(byte[] arrBytes)
            {
                MemoryStream memStream = new MemoryStream();
                BinaryFormatter binForm = new BinaryFormatter();
                memStream.Write(arrBytes, 0, arrBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                Object obj = (Object)binForm.Deserialize(memStream);
                return obj;
            }
 
            public static string _path="";

            public static string pathToDataBase = "SERVER=localhost;DATABASE=pathfinder;UID=root;PASSWORD=123;";


            //voobwe - ispoljzivatj toljko tip metabolit!!!


            //sdelatj formu sravnenija i vibora danih po metabolitam dlja vibrannih putej, sohranenie v fail

            //sravnenie reakcij

            //poisk genov dlja nuzhnih reakcij i podskazka etih genov useru  (eto mozhno pered i posle fba). podkskazki cto mozhno ewjo delatj s nimi

            //dobavitj funkcional - posik putej ne toljko dlja vibronoga soursa, no takze dlja produkta. znacit zapustitj progu ewjko raz, cto bo ona mogla bi posmotretj kakie reakcii dolzni bitj dobavleni cto soedenitj modelj s produktom.

            //v konce pokazaj polnuij putj - ne toljko dobavelnnij, no do produkta

            //vizualizacija - odin sposob prikrutitj grapfviz - s pokazom infi. dodelatj WPF s pokazom infi elememntov.

            //pereesti vsjo na objekti - kazdij metabolit objekt - pri sravnivanii infi sozdavatj novij metabolit v kotorij vpisivatj nuzhnuju infu,

            //perenesti reakcii i metaboliti v obwij klass elementov/objektov

            //struktirozirovatj proggramu po failam, dobavitj koment i cto bi ne putatsja

    

            //SYNBIO
            //scitivatj iz partsregistry iili biofab
        // cto scitivatj? kak zadavatj parametri cto hotim sozdatj? //SBOL

          
            //todo pocititj nenuzhnie polja, sdelatj zapisj v fail, razobratsa s charged i necharged formuloj



            //get data from kegg by ec number
            public static string GetKeddReactionIDDataByEC(string ec_number)
            {
                //create document to return
                XmlDocument xmlDoc = new XmlDocument();

                //url of the service
                string url = "http://www.genome.jp/dbget-bin/www_bget?" + ec_number;

                // used to build entire input
                StringBuilder sb = new StringBuilder();

                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)
                    WebRequest.Create(url);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?

                string s = sb.ToString();

                string reaction = "";
                try
                {
                    //44 - [RN:<a href="/dbget-bin/www_bget?rn:R01528">R01528</a>] get only reaction id
                    reaction = "rn:" + s.Substring(s.IndexOf("[RN:") + 44, 6);
                }
                catch (Exception ex)
                {
                }
                return reaction;
            }

            public static List<chebiObject> GetChebiDataByKeggCompound(string keggId)
            {
                List<chebiObject> listOfObjects = new List<chebiObject>();
                //http://www.ebi.ac.uk/chebi/advancedSearchFT.do?searchString=c00184
                chebiObject chebiObj = new chebiObject();
                //
                chebi.ChebiWebServiceService client = new chebi.ChebiWebServiceService();

                var lite = new chebi.getLiteEntity();

                lite.maximumResults = 5;

                lite.search = keggId;

                lite.searchCategory = chebi.SearchCategory.DATABASELINKREGISTRYNUMBERCITATION;
                lite.stars = chebi.StarsCategory.THREEONLY;

                var entities = new chebi.getLiteEntityResponse();
                try
                {

                     entities = client.getLiteEntity(lite);
                }

                catch (InvalidOperationException e)
                {
                     entities = client.getLiteEntity(lite);
                }

                foreach (var e in entities.@return)
                {
                    var ent = new chebi.getCompleteEntity();
                    ent.chebiId = e.chebiId;
                    chebi.getCompleteEntityResponse entity = client.getCompleteEntity(ent);

                     


                    var tmp = new chebiObject();

                    if (entity.@return.chebiId != null)
                        tmp.chebiID = entity.@return.chebiId.Substring(6);

                    if (entity.@return.charge != null)
                        tmp.charge = entity.@return.charge;

                    if (entity.@return.Formulae != null)
                    foreach (chebi.DataItem formula in entity.@return.Formulae)
                        tmp.formula.Add(formula.data);

                    if (entity.@return.inchi != null)
                    tmp.inchi = entity.@return.inchi;

                   
                    tmp.keggID = keggId;

                    if (entity.@return.chebiAsciiName != null)
                    tmp.Name = entity.@return.chebiAsciiName;

                    if (entity.@return.OntologyParents != null)
                    foreach (chebi.OntologyDataItem op in entity.@return.OntologyParents)
                        tmp.parent_ontology.Add(op.chebiId + " " + op.chebiName);

                    if (entity.@return.OntologyChildren != null)
                    foreach (chebi.OntologyDataItem oc in entity.@return.OntologyChildren)
                        tmp.children_ontology.Add(oc.chebiId + " " + oc.chebiName);

                    if (entity.@return.SecondaryChEBIIds != null)
                    foreach (string sc in entity.@return.SecondaryChEBIIds)
                        tmp.secondaryChebiID.Add(sc.ToString());

                    if (entity.@return.DatabaseLinks != null)
                    foreach (chebi.DataItem dl in entity.@return.DatabaseLinks)
                        tmp.databas_link.Add(dl.data + " " + dl.type);

                    if (entity.@return.smiles != null)
                    tmp.smiles = entity.@return.smiles;

                    if (entity.@return.Synonyms != null)
                    foreach (chebi.DataItem s in entity.@return.Synonyms)
                        tmp.synonimus.Add(s.data);

                    tmp.link = "http://www.ebi.ac.uk/chebi/advancedSearchFT.do?searchString=" + tmp.chebiID;

                    listOfObjects.Add(tmp);
                }

                return listOfObjects;

            }

        
            //search pubChemId by keggID
            private static string searchPubChemIDbyKeggID(string keggID)
            {
                HttpWebRequest request = (HttpWebRequest)
                   WebRequest.Create("http://www.ncbi.nlm.nih.gov/pccompound?term=" + keggID);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tmp = response.ResponseUri.ToString();

                return tmp.Substring(tmp.IndexOf("=") + 1);
            }

            //get PubChem data
            public static pubChemObject GetPubChemDataByKeggCompound(string keggID)
            {
                //full "http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=439191";

                string pubChemID = searchPubChemIDbyKeggID(keggID);

                //url of the service
                string Computed_Properties = "http://pubchem.ncbi.nlm.nih.gov/toc/summary_toc.cgi?tocid=27&cid=" + pubChemID;
                string Related = "http://pubchem.ncbi.nlm.nih.gov/toc/summary_toc.cgi?tocid=402&cid=" + pubChemID;
                string Compound_Information = "http://pubchem.ncbi.nlm.nih.gov/toc/summary_toc.cgi?tocid=397&cid=" + pubChemID;
                string Synonyms = "http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?q=nama&cid=" + pubChemID;


                pubChemObject pubChem = new pubChemObject();

                pubChem.pubchemID = pubChemID;

                pubChem.link = "http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=" + pubChemID;

                string responseComputed_Properties = GetURLData(Computed_Properties);
              //  string responseRelated = GetURLData(Related);
                string responseCompound_Information = GetURLData(Compound_Information);
                string responseSynonyms = GetURLData(Synonyms);

                //parse computed properties
                List<string> tmp = new List<string>();

                string[] arr = responseComputed_Properties.Split(new string[] { "<td", "</td>", "<tr", "</tr>" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in arr)
                {
                    if (!s.Contains("onmouseover") && !s.Contains(" style=") && s.Length != 1) tmp.Add(s);
                }

                pubChem.formula.Add(tmp[tmp.IndexOf(">Molecular Formula") + 1].Substring(1).Replace("<sub>", "").Replace("</sub>", ""));
                pubChem.charge = tmp[tmp.IndexOf(">Formal Charge") + 1].Substring(1);



                //parse responseCompound_Information
                
                tmp = new List<string>();

                arr = responseCompound_Information.Split(new string[] { "<div ", "<b>", "</b>", "</div>" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in arr)
                {
                    if (!s.Contains("onmouseover") && !s.Contains(" style=") && s.Length != 1)
                        tmp.Add(s);
                }



                pubChem.smiles = tmp[tmp.IndexOf("Canonical SMILES : ") + 1];

                pubChem.inchi = tmp[tmp.IndexOf("InChI: ") + 1];

                pubChem.keggID = keggID;
                
                

                //parse Synonyms
                
                tmp = new List<string>();

                arr = responseSynonyms.Split(new string[] { "</a><br />", "\">", "<a href=", "&nbsp", "\n</br>", ";" ,"</a>"}, StringSplitOptions.RemoveEmptyEntries);


               //fill synonimus and chebiID
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Contains("www") && !arr[i].Contains("script") && !arr[i].Contains("&nbsp") && !arr[i].Contains("</br>") && !arr[i].Contains(" "))
                    {

                        if (pubChem.synonimus.Count<10) pubChem.synonimus.Add(arr[i + 1]);
                        if (arr[i].Contains("CHEBI")) pubChem.chebiID = arr[i+1].Substring(6);
                    }

                       
                }

                //fill name - first in synonimus
                pubChem.Name = pubChem.synonimus[0];
                 

                return pubChem;

            }



            //get Kegg data
            public static Metabolites GetKeggDataByCompound(string keggID)
            {
                string kegg_data = GetMessageFromWebsite(keggID, "kegg");

                Metabolites compound = new Metabolites();

                compound.Link = "http://rest.kegg.jp/get/" + keggID;

                //parse computed properties
                string[] charSeparators = new string[] { "\n", "    " };

                string[] arr = kegg_data.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

                int entry = 0;
                int name = 0;
                int formula = 0;
                int exact_mass = 0;
                int reaction = 0;
                int pathway = 0;
                int dblink = 0;
                int atom = 0;

                for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i].Contains("ENTRY")) { entry = i; }
                    if (arr[i].Contains("NAME")) { name = i; }
                    if (arr[i].Contains("FORMULA")) { formula = i; }
                    if (arr[i].Contains("EXACT_MASS")) { exact_mass = i; }
                    if (arr[i].Contains("REACTION")) { reaction = i; }
                    if (arr[i].Contains("PATHWAY")) { pathway = i; }
                    if (arr[i].Contains("DBLINKS")) { dblink = i; }
                    if (arr[i].Contains("ATOM")) { atom = i; }
                }

                for (int i = name + 1; i < formula; i++)
                    compound.Name += arr[i];

                compound.KEGG_ID = arr[entry + 1];


                List<string> tmp = new List<string>();
                for (int i = dblink + 1; i < atom; i++)
                    tmp.Add(arr[i]);
                
                foreach (string s in tmp)
                {
                    if (s.Contains("PubChem"))
                        compound.PubChem_ID = s.Substring(8);
                }
                foreach (string s in tmp)
                {
                    if (s.Contains("ChEBI"))
                        compound.ChEBI_ID = s.Substring(6);
                }

                compound.Formula_charged = arr[formula + 1];

                return compound;
            }



            //get web page by URL
            private static string GetURLData(string _url)
            {
                //create document to return
                XmlDocument xmlDoc = new XmlDocument();

                string url = _url;

                // used to build entire input
                StringBuilder sb = new StringBuilder();

                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)
                    WebRequest.Create(url);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?

                string resultString = sb.ToString();


                return resultString;
            }

            //get message form KEGG REST
            //the same as GetURLData
            private static string GetMessageFromWebsite(string keggID, string database)
            {

                string url = "";

                if (database == "kegg")
                    url = "http://rest.kegg.jp/get/" + keggID;
                if (database == "chebi")
                    url = "http://www.ebi.ac.uk/chebi/advancedSearchFT.do?searchString=" + keggID + "&submit=Search+ChEBI&queryBean.stars=3";
                        //"http://www.ebi.ac.uk/chebi/searchId.do;536442C08F253F094DB76BBC8D2F32AD?printerFriendlyView=true&locale=null&chebiId=16016&viewTermLineage=null&structureView=&";
                        //"http://www.ebi.ac.uk/chebi/searchId.do;536442C08F253F094DB76BBC8D2F32AD?chebiId=CHEBI:16016";
                        //+"&submit=Search+ChEBI&queryBean.stars=2";
               
                HttpWebRequest request = CreateHttpRequest(url);

                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        var responseValue = string.Empty;

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                            throw new ApplicationException(message);
                        }

                        // grab the response
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                        }

                        //razbitj na tabi
                        return responseValue;
                    }
                }
                catch (WebException ex)
                {
                    try
                    {
                        using (var response = (HttpWebResponse)request.GetResponse())
                        {
                            var responseValue = string.Empty;

                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                                throw new ApplicationException(message);
                            }

                            // grab the response
                            using (var responseStream = response.GetResponseStream())
                            {
                                using (var reader = new StreamReader(responseStream))
                                {
                                    responseValue = reader.ReadToEnd();
                                }
                            }

                            //razbitj na tabi
                            return responseValue;
                        }
                    }
                    catch (WebException e)
                    {
                        MessageBox.Show("Failed to connect to database. TRY AGAIN "+ e);
                        return "";
                    }
                }
            }

            //create request
            private static HttpWebRequest CreateHttpRequest(string serviceEndPoint)
            {
                HttpWebRequest request =
                    (HttpWebRequest)WebRequest.Create(serviceEndPoint);
                request.Method = "GET";
                request.Accept = "application/atom+xml";
                return request;
            }

            //get data from kegg
            public static string GetKeddReactionPairsByKeggReactionID(string reaction, string ind)
            {
                //create document to return
                XmlDocument xmlDoc = new XmlDocument();

                //url of the service
                string url = "http://www.genome.jp/dbget-bin/www_bget?" + reaction;

                // used to build entire input
                StringBuilder sb = new StringBuilder();

                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)
                    WebRequest.Create(url);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?


                string s = sb.ToString();

                string rpair_st = "";
                List<string> rpair = new List<string>();

                string[] tmp1 = s.Split(new string[] { "<tr>" }, StringSplitOptions.None);

                foreach (string aaa in tmp1)
                {
                    if (aaa.Contains("<nobr>Equation</nobr>"))
                    {
                        //string[] r = aaa.Split(new string[] { "\">", "</a>", "<a href" }, StringSplitOptions.RemoveEmptyEntries);
                        //foreach (string tmp in r)
                        //{
                        //    if (!tmp.StartsWith("=") && !tmp.StartsWith("<"))
                        //        equation = equation + " " + tmp;
                        //}
                        //equation = equation.Replace("&lt;", "<");
                    }

                    if (aaa.Contains("<nobr>Definition</nobr>"))
                    {

                    }
                    if (aaa.Contains("<nobr>Name</nobr>"))
                    {

                    }
                    if (aaa.Contains("\">RP") && !aaa.Contains("leave") && !aaa.Contains("trans"))
                    {
                        //aaa.Contains("main") &&
                        if (aaa.Contains(ind.Substring(5)))
                        {
                            string tmp3 = aaa.Remove(0, aaa.IndexOf("left\">C") + 6);
                            tmp3 = tmp3.Remove(14);
                            rpair.Add(tmp3);

                            Write_Reaction_Compounds(reaction, "cpd:" + tmp3.Substring(0, 6));
                            Write_Reaction_Compounds(reaction, "cpd:" + tmp3.Substring(7));

                            rpair_st = rpair_st + " " + "cpd:" + tmp3;
                            //rpair_st = "cpd:" + rpair_st;
                            //  rpair_st = rpair_st.Insert(rpair_st.IndexOf(" C"), "cpd:");
                            rpair_st = rpair_st.Insert(rpair_st.LastIndexOf("C"), "cpd:");
                            rpair_st = rpair_st.Trim();

                        }

                    }
                }
                // print out page source
                //  Console.WriteLine(equation);
                return rpair_st;
            }

            //writes reactions
            public static void Write_Compound_Reacions(string _object, string[] _object_list)
            {
                MySqlConnection connection = new MySqlConnection(pathToDataBase);
                MySqlCommand command = connection.CreateCommand();

                try
                {
                    connection.Open();

                    foreach (string s in _object_list)
                    {
                        command.CommandText = "insert into compound_reaction (compound, reaction) values ('" + _object + "', '" + s + "')";
                        command.ExecuteScalar();
                    }

                    connection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            public static void Write_Reaction_Compounds(string _object, string _object_list)
            {


                MySqlConnection connection = new MySqlConnection(pathToDataBase);
                MySqlCommand command = connection.CreateCommand();

                try
                {
                    connection.Open();
                    command.CommandText = "insert into reaction_compound (reaction, compound) values ('" + _object + "', '" + _object_list + "')";
                    command.ExecuteScalar();
                    connection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }


            }


        }


        public class chebiObject
        {
            public string link
            {
                get;
                set;
            }

            public string chebiID
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public List<string> formula
            {
                get;
                set;
            }
            public string charge
            {
                get;
                set;
            }
            public string inchi
            {
                get;
                set;
            }
            public string smiles
            {
                get;
                set;
            }
            public List<string> synonimus
            {
                get;
                set;
            }
            public List<string> children_ontology
            {
                get;
                set;
            }
            public List<string> parent_ontology
            {
                get;
                set;
            }
            public List<string> databas_link
            {
                get;
                set;
            }

            public string keggID
            {
                get;
                set;
            }
            public List<string> secondaryChebiID
            {
                get;
                set;
            }

            public chebiObject()
            {
                formula = new List<string>();
                children_ontology = new List<string>();
                secondaryChebiID = new List<string>();
                parent_ontology = new List<string>();
                synonimus = new List<string>();
                databas_link = new List<string>();
            }
        }

        public class pubChemObject
        {
            public string link
            {
                get;
                set;
            }
            /// <summary>
            /// ??????
            /// </summary>
            public string pubchemID
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }
            public List<string> formula
            {
                get;
                set;
            }
            public string charge
            {
                get;
                set;
            }
            public string inchi
            {
                get;
                set;
            }
            public string smiles
            {
                get;
                set;
            }
            public List<string> synonimus
            {
                get;
                set;
            }
            public List<string> children_ontology
            {
                get;
                set;
            }
            public List<string> parent_ontology
            {
                get;
                set;
            }

            public string keggID
            {
                get;
                set;
            }

            public string additionalInfo
            {
                get;
                set;
            }
            /// <summary>
            /// ?????
            /// </summary>
            public string chebiID
            {
                get;
                set;
            }

            public pubChemObject()
            {
                formula = new List<string>();
                children_ontology = new List<string>();
               // secondaryChebiID = new List<string>();
                parent_ontology = new List<string>();
                synonimus = new List<string>();
            }
        }

        public class keggObject
        {

            public string link
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }
            public List<string> formula
            {
                get;
                set;
            }
            public List<string> reactions
            {
                get;
                set;
            }
            public string charge
            {
                get;
                set;
            }

            public List<string> synonimus
            {
                get;
                set;
            }

            public List<string> dbconn
            {
                get;
                set;
            }

            public string keggID
            {
                get;
                set;
            }

            public string chebiID
            {
                get;
                set;
            }

            public string pubchemID
            {
                get;
                set;
            }


            public keggObject()
            {
                formula = new List<string>();
                dbconn = new List<string>();
                synonimus = new List<string>();
                reactions = new List<string>();
            }
        }

        public class compoundInformation
        {
            public string link
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }
            public string formula
            {
                get;
                set;
            }
            public string charge
            {
                get;
                set;
            }
            public string inchi
            {
                get;
                set;
            }
            public string smiles
            {
                get;
                set;
            }
 

            public string keggID
            {
                get;
                set;
            }
            public string chebiID
            {
                get;
                set;
            }
            public string abbreviation
            {
                get;
                set;
            }
            public string compartment
            {
                get;
                set;
            }
            public string formulaNeutral
            {
                get;
                set;
            }
            public string info
            {
                get;
                set;
            }
            public string pubchemid
            {
                get;
                set;
            }
            public compoundInformation()
            {

            }

            //todo v shared funcitions
            public bool FillCompoundDataToGridView(string compound)
            {
            

                List<string> list = new List<string>();

                MySqlConnection connection = new MySqlConnection(shared_functions.pathToDataBase);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader reader;

                command.CommandText = "select * from  compound_information where keggid ='" + compound + "'";

                bool readed=false; 

                try
                {
                    connection.Open();

                    reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        this.link = reader.GetValue(1).ToString();
                        this.Name = reader.GetValue(2).ToString();
                        this.formula = reader.GetValue(3).ToString();
                        this.formulaNeutral = reader.GetValue(4).ToString();
                        this.charge = reader.GetValue(5).ToString();
                        this.compartment = reader.GetValue(6).ToString();
                        this.keggID = reader.GetValue(7).ToString();
                        this.chebiID = reader.GetValue(8).ToString();
                        this.pubchemid = reader.GetValue(9).ToString();
                        this.inchi = reader.GetValue(10).ToString();
                        this.smiles = reader.GetValue(11).ToString();
                        this.abbreviation = reader.GetValue(12).ToString();


                        readed = true;
      
             
                        ///list.Add(reader.GetValue(2).ToString());
                    }

                    connection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                return readed;

            }
           

            //todo v shared funcitions
            public void WriteCompoundDataToGridView(string compound)
            {

                //string MyConString = "SERVER=localhost;DATABASE=pathfinder;UID=root;PASSWORD=123;";

                //MySqlConnection connection = new MySqlConnection(MyConString);
                //MySqlCommand command = connection.CreateCommand();

                //try
                //{
                //    connection.Open();

                //    foreach (string s in _object_list)
                //    {
                //        command.CommandText = "insert into compound_reaction (compound, reaction) values ('" + _object + "', '" + s + "')";
                //        command.ExecuteScalar();
                //    }

                //    connection.Close();
                //}
                //catch (MySqlException ex)
                //{
                //    MessageBox.Show(ex.ToString());
                //}
            }
           

        }


        public class reactionInformation
        {
            public void FillReactionDataToGridView(string compound)
            {

            }

            public void WriteReactionDataToGridView(string compound)
            {

            }
        }

    }
}
